package com.jhgsys.internal.incentive.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.incentive.dto.IncentiveQuotaApplyDto;
import com.jhgsys.internal.incentive.entity.IncentiveQuotaApply;
import com.jhgsys.internal.incentive.entity.IncentiveQuotaConfig;
import com.jhgsys.internal.incentive.service.IIncentiveQuotaApplyService;
import com.jhgsys.internal.incentive.service.IIncentiveQuotaConfigService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 *  Controller
 *
 * @author zhangwei
 * @date 2024-09-03 13:16:21
 */
@Slf4j
@Validated
@Controller
public class IncentiveQuotaApplyController extends BaseController {

    @Autowired
    private IIncentiveQuotaApplyService incentiveQuotaApplyService;

    @Autowired
    private IIncentiveQuotaConfigService incentiveQuotaConfigService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveQuotaApply")
    @RequiresPermissions("incentiveQuotaApply:view")
    public String incentiveQuotaApplyIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("currentTenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        return FebsUtil.view("incentive/incentiveQuotaApply/incentiveQuotaApplyList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveQuotaApply/add")
    @RequiresPermissions("incentiveQuotaApply:add")
    public String incentiveQuotaApplyAdd(){
        return FebsUtil.view("incentive/incentiveQuotaApply/incentiveQuotaApplyEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveQuotaApply/update/{id}")
    @RequiresPermissions("incentiveQuotaApply:edit")
    public String incentiveQuotaApplyUpdate(Model model, @PathVariable("id") String id){
        IncentiveQuotaApplyDto incentiveQuotaApply = incentiveQuotaApplyService.findIncentiveQuotaApplyById(id);
        model.addAttribute("incentiveQuotaApply", incentiveQuotaApply);
        return FebsUtil.view("incentive/incentiveQuotaApply/incentiveQuotaApplyEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveQuotaApply/audit/{id}")
    @RequiresPermissions("incentiveQuotaApply:audit")
    public String incentiveQuotaApplyAudit(Model model, @PathVariable("id") String id){
        IncentiveQuotaApplyDto incentiveQuotaApply = incentiveQuotaApplyService.findIncentiveQuotaApplyById(id);
        model.addAttribute("incentiveQuotaApply", incentiveQuotaApply);
        return FebsUtil.view("incentive/incentiveQuotaApply/incentiveQuotaApplyAudit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveQuotaApply/detail/{id}")
    @RequiresPermissions("incentiveQuotaApply:edit")
    public String incentiveQuotaApplyDetail(Model model, @PathVariable("id") String id) {
        IncentiveQuotaApplyDto incentiveQuotaApply = incentiveQuotaApplyService.findIncentiveQuotaApplyById(id);
        model.addAttribute("incentiveQuotaApply", incentiveQuotaApply);
        return FebsUtil.view("incentive/incentiveQuotaApply/incentiveQuotaApplyDetail");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveQuotaApplyAudit")
    @RequiresPermissions("incentiveQuotaApply:audit")
    public String incentiveQuotaApplyAuditList(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("currentTenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        return FebsUtil.view("incentive/incentiveQuotaApply/incentiveQuotaApplyAuditList");
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-09-03 13:16:21
     */
    @GetMapping("incentiveQuotaApply")
    @ResponseBody
    @RequiresPermissions("incentiveQuotaApply:view")
    public FebsResponse getAllIncentiveQuotaApplys(IncentiveQuotaApply incentiveQuotaApply) {
        return new FebsResponse().success().data(incentiveQuotaApplyService.findIncentiveQuotaApplyList(incentiveQuotaApply));
    }

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-09-03 13:16:21
     */
    @GetMapping("incentiveQuotaApply/list/{type}")
    @ResponseBody
    @RequiresPermissions("incentiveQuotaApply:view")
    public FebsResponse incentiveQuotaApplyList(QueryRequest request, IncentiveQuotaApply incentiveQuotaApply,@PathVariable("type") String type) {
        if(type.equals("normalList")){
            incentiveQuotaApply.setUserId(getCurrentUserId());
        }
        Map<String, Object> dataTable = getDataTable(this.incentiveQuotaApplyService.findIncentiveQuotaApplyPage(request, incentiveQuotaApply));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 IncentiveQuotaApply
     * @author zhangwei
     * @date 2024-09-03 13:16:21
     */
    @Log("新增IncentiveQuotaApply")
    @PostMapping("incentiveQuotaApply")
    @ResponseBody
    @RequiresPermissions("incentiveQuotaApply:add")
    public FebsResponse addIncentiveQuotaApply(@RequestBody IncentiveQuotaApply incentiveQuotaApply){
        try {
            IncentiveQuotaConfig query = new IncentiveQuotaConfig();
            query.setYear(Calendar.getInstance().get(Calendar.YEAR));
            query.setUserId(getCurrentUserId());
            query.setStatus("1");
            if(null == incentiveQuotaConfigService.findIncentiveQuotaConfig(query)){
                return failure("开单人额度未设置");
            }
            //必填校验
            this.checkNecessaryColumns(incentiveQuotaApply,false);
            incentiveQuotaApply.setCreatedBy(getCurrentUser().getUserName());
            if(StringUtils.isBlank(incentiveQuotaApply.getUserId())){
                incentiveQuotaApply.setUserId(getCurrentUserId());
                incentiveQuotaApply.setUserNo(getCurrentUser().getUserNo());
                incentiveQuotaApply.setRealName(getCurrentUser().getUname());
                incentiveQuotaApply.setDeptId(getCurrentUser().getDeptId());
                incentiveQuotaApply.setDeptName(getCurrentUser().getDeptName());
            }
            this.incentiveQuotaApplyService.createIncentiveQuotaApply(incentiveQuotaApply);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增IncentiveQuotaApply失败";
            log.error(message, e);
            return failure(message);
        }
    }

    /**
     * 逻辑删除 IncentiveQuotaApply
     * @author zhangwei
     * @date 2024-09-03 13:16:21
     */
    @Log("逻辑删除IncentiveQuotaApply")
    @GetMapping("incentiveQuotaApply/delete/{id}")
    @ResponseBody
    @RequiresPermissions("incentiveQuotaApply:delete")
    public FebsResponse deleteIncentiveQuotaApply(@PathVariable("id") String incentiveQuotaApplyIds){
        try {
            IncentiveQuotaApply incentiveQuotaApply = new IncentiveQuotaApply();
            incentiveQuotaApply.setDeletedBy(getCurrentUser().getUserName());
            incentiveQuotaApply.setDeletedTime(new Date());
            incentiveQuotaApply.setDeletedFlg(FebsConstant.INVALID);
            this.incentiveQuotaApplyService.deleteIncentiveQuotaApply(incentiveQuotaApply,incentiveQuotaApplyIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除IncentiveQuotaApply失败";
            log.error(message, e);
            return failure(message);
        }
    }

    /**
    * 物理删除 IncentiveQuotaApply
    * @author zhangwei
    * @date 2024-09-03 13:16:21
    */
    @Log("物理删除IncentiveQuotaApply")
    @GetMapping("incentiveQuotaApply/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("incentiveQuotaApply:delete")
    public FebsResponse physicallyDeleteIncentiveQuotaApply(@PathVariable("id") String incentiveQuotaApplyIds) {
        try {
            this.incentiveQuotaApplyService.physicallyDeleteIncentiveQuotaApply(incentiveQuotaApplyIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除IncentiveQuotaApply失败";
            log.error(message, e);
            return failure(message);
        }
    }

    /**
     * 修改 IncentiveQuotaApply
     * @author zhangwei
     * @date 2024-09-03 13:16:21
     */
    @Log("修改IncentiveQuotaApply")
    @PostMapping("incentiveQuotaApply/update")
    @ResponseBody
    @RequiresPermissions("incentiveQuotaApply:edit")
    public FebsResponse updateIncentiveQuotaApply(@RequestBody IncentiveQuotaApply incentiveQuotaApply){
        try {
            //必填校验
            this.checkNecessaryColumns(incentiveQuotaApply,true);
            incentiveQuotaApply.setUpdatedBy(getCurrentUser().getUserName());
            incentiveQuotaApply.setStatus("2");
            this.incentiveQuotaApplyService.updateIncentiveQuotaApply(incentiveQuotaApply);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改IncentiveQuotaApply失败";
            log.error(message, e);
            return failure(message);
        }
    }

    /**
     * 审核 IncentiveQuotaApply
     * @author zhangwei
     * @date 2024-09-03 13:16:21
     */
    @Log("审核IncentiveQuotaApply")
    @PostMapping("incentiveQuotaApply/auditApply")
    @ResponseBody
    @RequiresPermissions("incentiveQuotaApply:audit")
    public FebsResponse auditIncentiveQuotaApply(@RequestBody IncentiveQuotaApplyDto incentiveQuotaApplyDto){
        try {
            incentiveQuotaApplyDto.setAuditRealName(getCurrentUser().getUname());
            incentiveQuotaApplyDto.setAuditUserId(getCurrentUserId());
            this.incentiveQuotaApplyService.auditIncentiveQuotaApply(incentiveQuotaApplyDto);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改IncentiveQuotaApply失败";
            log.error(message, e);
            return failure(message);
        }
    }

    private FebsResponse checkNecessaryColumns(IncentiveQuotaApply incentiveQuotaApply,boolean editFlg){
        if(editFlg && StringUtils.isBlank(incentiveQuotaApply.getQuotaApplyId())){
            return failure("主键不能为空");
        } else if(null == incentiveQuotaApply.getApplyQuota()){
            return failure("申请额度不能为空");
        } else if(StringUtils.isBlank(incentiveQuotaApply.getApplyRemark())){
            return failure("申请理由不能为空");
        }else{
            return success();
        }
    }

}
